package cn.buk.tms.eotms.request;

import cn.buk.tms.eotms.dto.AbstractBaseDto;
import cn.buk.tms.eotms.dto.HotelOrderPassengerDto;
import cn.buk.tms.eotms.dto.HotelOrderPriceDto;
import cn.buk.tms.eotms.entity.Customer;
import cn.buk.tms.eotms.entity.HotelOrder;
import cn.buk.tms.eotms.entity.HotelOrderPassenger;
import cn.buk.tms.eotms.entity.HotelOrderPrice;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 酒店订单DTO
 * Created by yfdai on 2018-10-17
 * @author yfdai
 */
public class HotelOrderRequestDto extends AbstractBaseDto {

    public static HotelOrder convertToHotelOrder(HotelOrderRequestDto dto) {
        HotelOrder o = new HotelOrder();
        BeanUtils.copyProperties(dto, o, "passengers", "prices");

        String psgNames = null;
        for(HotelOrderPassengerDto psgDto: dto.getPassengers()) {

            HotelOrderPassenger psg = new HotelOrderPassenger();
            psg.setOrderInfo(o);
            psg.setEnterpriseId(o.getEnterpriseId());

            BeanUtils.copyProperties(psgDto, psg);

            o.getPassengers().add(psg);

            if (psgNames == null) {
                psgNames = psgDto.getName();
            } else {
                psgNames += "," + psgDto.getName();
            }
        }

        o.setPsgCount(o.getPassengers().size());
        if (psgNames != null && psgNames.length() > 50) {
            psgNames = psgNames.substring(0, 45) + "...";
        }
        o.setPassengerName(psgNames);

        o.setCustomer(null);
        if (dto.getCustomerId() > 0) {
            Customer c = new Customer();
            c.setId(dto.getCustomerId());

            o.setCustomer(c);
            //企业客户
            o.setCustomerType(Customer.ENT_CORPORATION);
        } else {
            // 散客
            o.setCustomerType(Customer.ENT_INDIVIDUAL);
        }

        o.setPrices(null);
        for (HotelOrderPriceDto priceDto: dto.getPrices()) {
            HotelOrderPrice price = new HotelOrderPrice();
            price.setOrderInfo(o);
            price.setEnterpriseId(o.getEnterpriseId());

            BeanUtils.copyProperties(priceDto, price);

            o.getPrices().add(price);
        }

        return o;
    }

    /**
     * 计算总价
     */
    public void calc() {
        if (roomCount == 0 || this.getPrices().size() == 0) {
            return;
        }

        final int roomCount = this.roomCount;
        double total = 0;
        for(HotelOrderPriceDto priceDto: this.getPrices()) {
            total += priceDto.getInclusiveAmount() * roomCount;
        }

        //入住天数，和价格详情数一样
        final int nightCount = this.getPrices().size();

        this.totalAmount = total;
        this.price = this.totalAmount / (roomCount * nightCount);

        //保留两位数
        this.totalAmount = BigDecimal.valueOf(total).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        this.price = BigDecimal.valueOf(this.price).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 客户ID：
     * 0 - 散客
     * >1 - 客户（企业散客，企业客户）
     */
    private Integer customerId;

    /**
     * 客户名称（用于前台显示）
     */
    private String customerName;

    /**
     * 客户代码（用于前台显示）
     */
    private String customerCode;


    /**
     * 成本中心
     */
    private String costCenter;

    /**
     * 项目名称
     */
    private String projectName;

    /**
     * 付款方式
     */
    private int payType;

    //姓名 入住日期 酒店名称 房型 价格 服务费
    /**
     * 姓名
     */
    private String passengerName;

    /**
     * 入住人数
     */
    private int psgCount;

    /**
     * 入住人信息
     */
    private List<HotelOrderPassengerDto> passengers = new ArrayList<>();

    /**
     * 本地酒店系统中的酒店id
     */
    private int hotelId;


    /**
     * 酒店名称
     */
    private String hotelName;

    /**
     * 本地酒店系统中的售卖房型id
     */
    private int roomId;

    /**
     * 房型
     */
    private String roomType;

    /**
     * 房间数
     */
    private int roomCount;

    /**
     * 入住日期
     */
    private Date checkInDate;

    /**
     * 离店日期
     */
    private Date checkOutDate;

    /**
     * 价格/间夜(均价）
     */
    private double price;

    /**
     * 服务费
     */
    private double serviceCharge;

    /**
     * 间夜数(房间数*天数）
     */
    private Integer count;

    /**
     * 实收客户总额
     */
    private Double totalAmount;

    /**
     * 每日价格
     */
    @NotNull
    @Size(min = 0, max = 1000)
    private List<HotelOrderPriceDto> prices = new ArrayList<>();

    /**
     * 最晚到店时间
     */
    private String lastArriveTime;

    private String remark;

    /**
     * 订单联系人
     */
    @NotNull
    private String linkman;

    /**
     * 订单联系电话
     */
    @NotNull
    private String contactPhone;

    public void processPassengerInfo() {

        String psgNames = null;
        for(HotelOrderPassengerDto psg: this.getPassengers()) {
            if (psgNames == null) {
                psgNames = psg.getName();
            } else {
                psgNames += "," + psg.getName();
            }
        }

        if (psgNames != null) {
            if (psgNames.length() > 50) {
                psgNames = psgNames.substring(0, 40);
            }

            this.passengerName = psgNames;
            this.psgCount = this.getPassengers().size();
        }
    }


    public String getPassengerName() {
        return passengerName;
    }

    public void setPassengerName(String passengerName) {
        this.passengerName = passengerName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getServiceCharge() {
        return serviceCharge;
    }

    public void setServiceCharge(double serviceCharge) {
        this.serviceCharge = serviceCharge;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public void setHotelName(String hotelName) {
        this.hotelName = hotelName;
    }

    public void setRoomType(String roomType) {
        this.roomType = roomType;
    }

    public String getRoomType() {
        return roomType;
    }

    public String getHotelName() {
        return hotelName;
    }

    public int getRoomCount() {
        return roomCount;
    }

    public void setRoomCount(int roomCount) {
        this.roomCount = roomCount;
    }

    public String getCostCenter() {
        return costCenter;
    }

    public void setCostCenter(String costCenter) {
        this.costCenter = costCenter;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public int getCount() {
        return count == null ? 1 : count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public double getTotalAmount() {
        return totalAmount == null ? 0 : totalAmount;
    }

    public void setTotalAmount(Double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public Date getCheckInDate() {
        return checkInDate;
    }

    public void setCheckInDate(Date checkInDate) {
        this.checkInDate = checkInDate;
    }

    public Date getCheckOutDate() {
        return checkOutDate;
    }

    public void setCheckOutDate(Date checkOutDate) {
        this.checkOutDate = checkOutDate;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerCode(String customerCode) {
        this.customerCode = customerCode;
    }

    public String getCustomerCode() {
        return customerCode;
    }


    public List<HotelOrderPassengerDto> getPassengers() {
        if (this.passengers == null) {
            this.passengers = new ArrayList<>();
        }
        return passengers;
    }

    public void setPassengers(List<HotelOrderPassengerDto> passengers) {
        this.passengers = passengers;
    }

    public int getPsgCount() {
        return psgCount;
    }

    public void setPsgCount(int psgCount) {
        this.psgCount = psgCount;
    }

    public String getLastArriveTime() {
        return lastArriveTime;
    }

    public void setLastArriveTime(String lastArriveTime) {
        this.lastArriveTime = lastArriveTime;
    }

    public List<HotelOrderPriceDto> getPrices() {
        if (prices == null) {
            prices = new ArrayList<>();
        }
        return prices;
    }

    public void setPrices(List<HotelOrderPriceDto> prices) {
        this.prices = prices;
    }

    public int getHotelId() {
        return hotelId;
    }

    public void setHotelId(int hotelId) {
        this.hotelId = hotelId;
    }

    public int getRoomId() {
        return roomId;
    }

    public void setRoomId(int roomId) {
        this.roomId = roomId;
    }

    public int getPayType() {
        return payType;
    }

    public void setPayType(int payType) {
        this.payType = payType;
    }

    public String getContactPhone() {
        return contactPhone;
    }

    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }

    public String getLinkman() {
        return linkman;
    }

    public void setLinkman(String linkman) {
        this.linkman = linkman;
    }
}
